from sklearn.datasets import load_boston

# 导入波斯顿的房价数据
boston = load_boston()


from sklearn.cross_validation import train_test_split
import numpy as np

# 提取出训练、测试集及目标值
X = boston.data
y = boston.target
# print(X)

# 随机采样25%的数据构建测试样本，其余作为训练样本
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.25)

# 分析回归目标值的差异
# print('目标值最大值：',np.max(y))
# print('目标值最小值:',np.min(y))
# print('目标平均值:',np.mean(y))


#  从sklearn.preprocessing导入数据标准化模块
from sklearn.preprocessing import StandardScaler

# 分别初始化对特征和目标值的标准化模块
ss_X = StandardScaler()
ss_y= StandardScaler()

# 分别对训练和测试数据的特征以及目标值进行标准化处理
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.fit_transform(X_test)
# y_train = ss_y.fit_transform(y_train)
# y_test = ss_y.transform(y_test)
y_train = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test = ss_y.transform(y_test.reshape(-1, 1))


# 从sklearn.svr中导入支持向量机(回归)模型
from sklearn.svm import SVR

# 使用线性核函数配置的支持向量机进行回归训练，并且对测试样本进行预测
linear_svr = SVR(kernel='linear')
linear_svr.fit(X_train, y_train.ravel())
linear_svr_y_predict = linear_svr.predict(X_test)
# print(linear_svr_y_predict)

# 使用多项式核函数配置的支持向量机进行回归训练，并且对测试样本进行预测
poly_svr = SVR(kernel='poly')
poly_svr.fit(X_train, y_train.ravel())
poly_svr_y_predict = poly_svr.predict(X_test)
# print(poly_svr_y_predict)

# 使用径向基核函数配置的支持向量机进行回归训练，并且对测试样本进行预测
rbf_svr = SVR(kernel='rbf')
rbf_svr.fit(X_train, y_train.ravel())
rbf_svr_y_predict = rbf_svr.predict(X_test)
# print(rbf_svr_y_predict)


# 对三种核函数配置下的支持向量机回归模型在相同测试集上进行性能评估
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
print('R-squared value of linear SVR is',linear_svr.score(X_test, y_test))
print('The mean squared error of linear SVR is',mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(linear_svr_y_predict)))
print('The mean absolute error of linear linear SVR is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(linear_svr_y_predict)))
print('-----------')

print('R-squared value of poly SVR is',poly_svr.score(X_test, y_test))
print('The mean squared error of poly SVR is',mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(poly_svr_y_predict)))
print('The mean absolute error of poly linear SVR is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(poly_svr_y_predict)))

print('-----------')
print('R-squared value of rbf SVR is',rbf_svr.score(X_test, y_test))
print('The mean squared error of rbf SVR is',mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rbf_svr_y_predict)))
print('The mean absolute error of rbf linear SVR is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rbf_svr_y_predict)))

# 通过三组性能评测我们发现，不同配置下的模型在相容的测试集上，存在着非常大的性能差异。并且在使用了径向基(Radial basis function)
# 核函数对特征进行非线性映射之后，支持向量机展现了最佳的回归性能
